{% extends 'base.html' %}

{% block title %}{{ title }} - Will's Blog{% endblock %}

{% block styles %}
<style>
    .editor-container {
        border: 1px solid #ddd;
        border-radius: 4px;
        margin-bottom: 15px;
    }
    .editor-toolbar {
        background: #f5f5f5;
        padding: 5px;
        border-bottom: 1px solid #ddd;
    }
    .editor-toolbar button {
        background: none;
        border: none;
        padding: 5px 10px;
        cursor: pointer;
    }
    .editor-toolbar button:hover {
        background: #e0e0e0;
    }
    #{{ form.body.id }} {
        width: 100%;
        min-height: 500px;
        padding: 10px;
        border: none;
        resize: vertical;
    }
</style>
{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-3">
        {% include 'admin/sidebar.html' %}
    </div>
    
    <div class="col-md-9">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h1>{{ title }}</h1>
            <a href="{{ url_for('admin.posts') }}" class="btn btn-secondary">
                <i class="bi bi-arrow-left"></i> 返回文章列表
            </a>
        </div>
        
        <div class="card">
            <div class="card-body">
                <form method="post" id="post-form">
                    {{ form.hidden_tag() }}
                    <input type="hidden" name="body" id="hidden-body">
                    
                    <div class="mb-3">
                        {{ form.title.label(class="form-label") }}
                        {{ form.title(class="form-control") }}
                        {% if form.title.errors %}
                            <div class="invalid-feedback d-block">
                                {% for error in form.title.errors %}
                                    {{ error }}
                                {% endfor %}
                            </div>
                        {% endif %}
                    </div>
                    
                    <div class="mb-3">
                        {{ form.body.label(class="form-label") }}
                        <div class="editor-container">
                            <div class="editor-toolbar">
                                <button type="button" onclick="insertText('**', '**')"><strong>B</strong></button>
                                <button type="button" onclick="insertText('_', '_')"><em>I</em></button>
                                <button type="button" onclick="insertText('# ', '')">H1</button>
                                <button type="button" onclick="insertText('## ', '')">H2</button>
                                <button type="button" onclick="insertText('- ', '')">•</button>
                                <button type="button" onclick="insertText('1. ', '')">1.</button>
                                <button type="button" onclick="insertText('> ', '')">></button>
                                <button type="button" onclick="insertText('`', '`')">`</button>
                                <button type="button" onclick="insertText('```\n', '\n```')">```</button>
                                <button type="button" onclick="insertText('[', '](url)')">Link</button>
                                <button type="button" onclick="insertText('![', '](image-url)')">Img</button>
                            </div>
                            {{ form.body(class="form-control", rows=15) }}
                        </div>
                        {% if form.body.errors %}
                            <div class="invalid-feedback d-block">
                                {% for error in form.body.errors %}
                                    {{ error }}
                                {% endfor %}
                            </div>
                        {% endif %}
                    </div>
                    
                    <div class="row mb-3">
                        <div class="col-md-6">
                            <div class="mb-3">
                                {{ form.tags.label(class="form-label") }}
                                {{ form.tags(class="form-control", placeholder="多个标签用逗号分隔") }}
                                {% if form.tags.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.tags.errors %}
                                            {{ error }}
                                        {% endfor %}
                                    </div>
                                {% endif %}
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                {{ form.published.label(class="form-label") }}
                                <div class="form-check form-switch">
                                    {{ form.published(class="form-check-input", role="switch") }}
                                    <label class="form-check-label" for="{{ form.published.id }}">
                                        {{ form.published.label.text }}
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="d-grid gap-2">
                        <button type="submit" class="btn btn-primary">保存</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
    function insertText(prefix, suffix) {
        const textarea = document.getElementById('{{ form.body.id }}');
        const start = textarea.selectionStart;
        const end = textarea.selectionEnd;
        const selectedText = textarea.value.substring(start, end);
        const beforeText = textarea.value.substring(0, start);
        const afterText = textarea.value.substring(end);
        
        textarea.value = beforeText + prefix + selectedText + suffix + afterText;
        
        // 设置光标位置
        const newCursorPos = start + prefix.length;
        textarea.selectionStart = textarea.selectionEnd = newCursorPos + selectedText.length;
        textarea.focus();
    }

    // 表单提交时同步内容
    document.getElementById('post-form').addEventListener('submit', function() {
        document.getElementById('hidden-body').value = document.getElementById('{{ form.body.id }}').value;
    });
</script>
{% endblock %}